$(document).ready(function () {
  var table = $('table#table').DataTable({
    ajax: {
      url: baseUrl + '/datatable',
      data : function(d) {
		var material = $('.search-form').find('input[name="material"]').val();
        var lotnum = $('.search-form').find('input[name="lotnum"]').val();
        var loccode = $('.search-form').find('input[name="loccode"]').val();
        var tracenum = $('.search-form').find('input[name="tracenum"]').val();
        var containnum = $('.search-form').find('input[name="containnum"]').val();
        var status = $('.search-form').find('input[name="status"]').val();
        d.material = material;
        d.lotnum = lotnum;
        d.loccode = loccode;
        d.tracenum = tracenum;
        d.containnum = containnum;
        d.status = status;
		return d;
      }
    },
    order: [
      [1, 'asc']
    ],
    columns: [{
      data: 'id',
      sortable: false,
      defaultContent: '',
      'class': 'text-center',
      render: function (data, type, row, meta) {
        return '<div class="checkbox"><input type="checkbox" name="ids" value="' + data + '"/><label for="ids"/></div>'
      }
     },{
      name: 'locationShortGroup',
      data: 'locationShortGroup'
    },{
      name: 'material.code',
      data: 'material.code'
    },{
      name: 'material.name',
      data: 'material.name'
    },{
      name: 'material.specModel',
      data: 'material.specModel'
    },{
      name: 'lotno.lotattr05',
      data: 'lotno.lotattr05'
    },{
      name: 'qty',
      data: 'qty'
    },{
      name: 'qtyAllocated',
      data: 'qtyAllocated'
    },{
      name: 'containNumber',
      data: 'containNumber'
    },{
      name: 'locationCode',
      data: 'locationCode'
    },{
      name: 'receiveTime',
      data: 'receiveTime'
    },{
      name: 'expireDate',
      data: 'expireDate'
    },{
      name: 'qualityStatus',
      data: 'qualityStatus'
    },{
      name: 'udf12',
      data: 'udf12'
    },{
      name: 'status',
      data: 'status',
      render: function (data, type, row, meta) {
        if (data=='0000') return '创建';
        if (data=='0200') return '已释放(WCS)';
        if (data=='0280') return '任务完成(WCS反馈)';
        if (data=='0880') return '任务结束';
        if (data=='0850') return '任务取消';
        if (data=='AVA') return '可用';
        if (data=='LCK') return '锁定';
      }
    }, {
      data: "id",
      defaultContent: '',
      sortable: false,
      'class': 'text-center',
      render: function (data, type, row, meta) {
        return ('<div class="btn-group">' +
          '<a  class="btn btn-default btn-xs" title="修改" href="' + baseUrl + '/edit/' + row.id + '"><i class="fa fa-edit"></i></a>' +
          '<a  class="btn btn-default btn-xs" title="调整" href="' + baseUrl + '/adjreq/' + row.id + '"><i class="fa fa-edit"></i></a>' +
        '</div>' +
          '&nbsp;&nbsp;');
      }
    }]
  });

  table.on('click', '.del', function () {
    var id = table.row($(this).parents('tr')).data().id;
    $("#delForm #ids").val(id);
    if (id) {
      bootbox.confirm("确定要删除数据吗?", function (result) {
        if (result) {
          $("#delForm").submit();
        }
      });
    }
  });
  $(".btn.delAll").on("click", function (e) {
    var ids = [];
    $('#table>tbody input:checked').each(function (i, item) {
      ids.push($(item).val());
    });
    $("#delForm #ids").val(ids.join(","));
    if (ids.length > 0) {
      bootbox.confirm("确定要删除选中的数据吗?", function (result) {
        if (result) {
          $("#delForm").submit();
        }
      });
    } else {
      bootbox.alert("请选择要删除的数据.");
    }
  });

  $(".btn.move").on("click", function (e) {
    var ids = [];
    $('#table>tbody input:checked').each(function (i, item) {
      ids.push($(item).val());
    });
    $("#moveForm #ids").val(ids.join(","));
    if (ids.length > 0) {
        bootbox.confirm("确定要拆垛选中的数据吗?", function (result) {
            if (result) {
              $("#moveForm").submit();
            }
          });
    } else {
      bootbox.alert("请选择要拆垛的数据.");
    }
  });
  

  $(".btn.delmovereqAll").on("click", function (e) {
    var ids = [];
    $('#table>tbody input:checked').each(function (i, item) {
      ids.push($(item).val());
    });
    $("#delmovereqForm #ids").val(ids.join(","));
    if (ids.length > 0) {
        bootbox.confirm("确定要销库下架选中的数据吗?", function (result) {
            if (result) {
              $("#delmovereqForm").submit();
            }
          });
    } else {
      bootbox.alert("请选择要销库的数据.");
    }
  });
  

  table.on('click', '.enable', function () {
    var that = this;
    var data = table.row($(this).parents('tr')).data();
    var id = data.id;
    var enabled = data.enabled;
    bootbox.confirm("确定要更新选中的数据吗?", function(resul) {
	if (resul) {
	    $.get(baseUrl + "/enable", {
	      enabled: !enabled,
	      ids: id
	    }, function (res) {
	      if (res) {
	        data.enabled = !enabled;
	        $(that).find("i").removeClass("fa-" + (enabled ? "check-" : "") + "square-o").addClass("fa-" + (!enabled ? "check-" : "") + "square-o");
	        toastr.info('操作成功!');
	      }
	
	    });
	}});   
  });

  //导出 动态组织form提交
  $(".btn.export").on("click", function (e) {
	$("#exportForm .param").remove();
	var $frm = $("#exportForm");
　　　var array = $('.search-form').serializeArray();
　　　for (i = 0, length = array.length; i < length; i++) {
	　　　key = array[i].name;
		value = array[i].value;
		$frm.append($('<input class="param" type="hidden" name = "' + key + '" value = "' + value + '" />')); 
	}
	var params = table.ajax.params();
	$frm.append($('<input class="param" type="hidden" name="input" value = \'' + JSON.stringify(params) + '\' />')); 
	$frm.submit();
	// 阻止默认行为
	e.preventDefault();
	return false;  
  });
  
  $('.search-form').on("submit", function (e) {
    table.draw();
    //阻止表单submit
    return false;
  });

});
